//
//  CXTabBar.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/26.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXTabBar.h"
#import "CXTabBarItem.h"
#import "CXTabBarButton.h"
#import "CXPlayView.h"

@interface CXTabBar()<CXPlayViewDelegate>

@property (nonatomic, strong) CXTabBarButton * currentSelectedButton;///< 记住当前选中的

@property(nonatomic,strong)NSMutableArray * tabBarButtonArray;

@property(nonatomic,strong) AVPlayer * player;

@property(nonatomic,strong)CXPlayView * playView;

@property(nonatomic,weak) UIButton * playButton;

@end

@implementation CXTabBar

+(instancetype)tabBarWithFrame:(CGRect )frame items:(NSArray <CXTabBarItem *> *)items{
    return [[self alloc] initWithFrame:frame items:items];
    
}

-(instancetype)initWithFrame:(CGRect)frame items:(NSArray <CXTabBarItem *> *)items{
    
    if (self = [super initWithFrame:frame]) {
        [self cx_configTabBarButton:items];
    }
    
    self.playView = [[CXPlayView alloc] init];
    self.playView.delegate = self;
    [self addSubview:_playView];
    
    //    [self.playView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.bottom.mas_equalTo(0);
    //        make.centerX.mas_equalTo(0);
    //        make.width.mas_equalTo(65);
    //        make.height.mas_equalTo(70);
    //    }];
    
    self.backgroundImage = [UIImage imageNamed:@"tabbar_bg"];
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    for (CXTabBarButton * btn in self.tabBarButtonArray) {
        [self bringSubviewToFront:btn];
    }
    [self bringSubviewToFront:self.playView];
    
}


#pragma mark - private methods
-(void)cx_configTabBarButton:(NSArray <CXTabBarItem *> *)items{
    
    for (NSInteger i = 0; i < items.count; i++) {
        CXTabBarButton * btn = [CXTabBarButton tabBarButtonWithItem:items[i]];
        btn.tag = i;
        
        CGFloat X = (i >= 2) ? (self.width / (items.count + 1)) * (i +1) : (self.width / (items.count + 1)) * i;
        
        btn.frame = CGRectMake(X, 0, self.width / (items.count + 1), self.height);
        
        [btn cx_setupTabBarButtonLayout];
        
        [btn addTarget:self action:@selector(cx_didClickTabBarButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBarButtonArray addObject:btn];
        [self addSubview:btn];
        if (i == 0) {
            btn.selected = YES;
            self.currentSelectedButton = btn;
        }
    }
    
    
}

#pragma mark - PlayView的代理方法
- (void)playButtonDidClick:(BOOL)selected {
    // 按钮被点击方法, 判断按钮的selected状态
    if (selected) {
        [_player play];// 继续播放
    } else {
        [_player pause];//暂停播放
    }
}


#pragma mark - action methods
-(void)cx_didClickTabBarButton:(CXTabBarButton *)btn{
    
    self.currentSelectedButton.selected = NO;
    btn.selected = YES;
    self.currentSelectedButton = btn;
    //通知外界切换控制器
    if ([self.delegate respondsToSelector:@selector(tabBar:tabBarIndex:)]) {
        [self.delegate tabBar:self tabBarIndex:btn.tag];
    }
}

#pragma mark - 懒加载
-(NSMutableArray *)tabBarButtonArray{
    if (!_tabBarButtonArray) {
        _tabBarButtonArray = [NSMutableArray array];
    }
    return _tabBarButtonArray;
}
@end
